package xyf.p07.binsearch;

/**
 * ClassName: P0153_FindMin
 * Description:
 * Author: xyf
 * Date: 9/7/22 7:07 PM
 * Version: 1.0
 **/
public class P0153_FindMin
{
    public int findMin(int[] nums)
    {
        int start = 0;
        int end = nums.length - 1;
        while (start <= end)
        {
            int middle = (end - start) / 2 + start;

            if (start == end)
            {
                return middle;
            }

            if ((middle != 0 && nums[middle - 1] > nums[middle]) || middle == 0 && nums[middle] < nums[end])
            {
                return nums[middle];
            }

            if (nums[end] < nums[middle])
            {
                start = middle + 1;
            }
            else
            {
                end = middle - 1;
            }
        }
        return -1;
    }
}